//
//  CalendarView.m
//  WuJieLive
//
//  Created by 张瀚坤 on 2017/11/9.
//  Copyright © 2017年 zhk. All rights reserved.
//

#import "CalendarView.h"

@implementation CalendarView

-(void)awakeFromNib
{
    [super awakeFromNib];
    [self setupView];
}

- (void)setupView
{
    self.transform = CGAffineTransformTranslate(self.transform, 0, -CGRectGetMaxY(self.frame));
    [UIView animateWithDuration:0.5 animations:^{
        self.transform = CGAffineTransformIdentity;
    }];

    NSLocale *ch_zh_locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_Hans_CN"] ;
    self.pickerView.locale = ch_zh_locale;
    self.pickerView.datePickerMode = UIDatePickerModeDate;
    self.pickerView.maximumDate = [NSDate date];
    [self.pickerView addTarget:self action:@selector(dateChanged:) forControlEvents:(UIControlEventValueChanged)];

    self.currentBtn = self.endBtn;
    self.currentBtn.selected = YES;
}

- (void)dateChanged:(UIDatePicker *)picker
{
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyyy-MM-dd";
    self.currentDateString = [formatter stringFromDate:picker.date];
    if (self.endBtn.selected) {
        [self.endBtn setTitle:self.currentDateString forState:(UIControlStateNormal)];
    }else if(self.beginBtn.selected)
    {
        [self.beginBtn setTitle:self.currentDateString forState:(UIControlStateNormal)];
    }
}

#pragma mark 结束日期
- (IBAction)endAction:(UIButton *)sender {
    self.currentBtn.selected = NO;
    self.currentBtn = sender;
    self.currentBtn.selected = YES;
}

#pragma mark 开始日期
- (IBAction)beginAction:(UIButton *)sender {
    self.currentBtn.selected = NO;
    self.currentBtn = sender;
    self.currentBtn.selected = YES;
}

- (IBAction)removeAction:(UIButton *)sender {
    [self removeFromSuperview];
}
- (IBAction)cancleAction:(UIButton *)sender {
    [self removeFromSuperview];
}
- (IBAction)doneAction:(UIButton *)sender {
    self.CalendarViewBlock(self.currentDateString, self.currentDateString);
    [self removeFromSuperview];
}

@end
